/*
 * @author ${user}
 * @date ${year}-${month}-${day}
 */

package net.xdclass.controller;


import lombok.extern.slf4j.Slf4j;
import net.xdclass.annotation.RepeatSubmit;
import net.xdclass.constant.RedisKey;
import net.xdclass.controller.request.ConfirmOrderRequest;
import net.xdclass.controller.request.ProductOrderPageRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.ClientTypeEnum;
import net.xdclass.enums.ProductOrderPayTypeEnum;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.service.ProductOrderService;
import net.xdclass.util.CommonUtil;
import net.xdclass.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2025-09-23
 */
@Slf4j
@RestController
@RequestMapping("/api/order/v1")
public class ProductOrderController {
	@Autowired
	private ProductOrderService productOrderService;

	@Autowired
	private StringRedisTemplate redisTemplate;

	@PostMapping("/page")
	@RepeatSubmit(limitType= RepeatSubmit.Type.PARAM)
	public JsonData page(@RequestBody ProductOrderPageRequest productOrderPageRequest){
		Map<String, Object> mapResult = productOrderService.getPage(productOrderPageRequest);
		return JsonData.buildSuccess(mapResult);
	}

	@GetMapping("/query_state")
	public JsonData queryState(@RequestParam String outTradeNo){
       String stateResult = productOrderService.queryState(outTradeNo);
       // JsonData.buildSuccess(stateResult);
       return StringUtils.isNotBlank(stateResult) ? JsonData.buildSuccess(stateResult) :JsonData.buildResult(BizCodeEnum.ORDER_CONFIRM_NOT_EXIST);
	}

	@PostMapping("/confirm_order")
	public JsonData confirmOrder(@RequestBody ConfirmOrderRequest confirmOrderRequest,
								 HttpServletResponse response){
		JsonData jsonData = productOrderService.confirmOrder(confirmOrderRequest,response);
		if ( jsonData.getCode() == 0 ){
			String clientType = confirmOrderRequest.getClientType();
			String payType    = confirmOrderRequest.getPayType();
			if( payType.equalsIgnoreCase(ProductOrderPayTypeEnum.ALI_PAY.name())){
				if (clientType.equalsIgnoreCase(ClientTypeEnum.APP.name())) {

				} else if (clientType.equalsIgnoreCase(ClientTypeEnum.H5.name())) {

				} else if (clientType.equalsIgnoreCase(ClientTypeEnum.PC.name())){

				}
				}
			 else if( payType.equalsIgnoreCase(ProductOrderPayTypeEnum.WECHAT_APY.name())){
                    productOrderService.confirmOrder(confirmOrderRequest,response);
			}
		}else {
               log.error("订单创建失败{}",jsonData.toString());
			   CommonUtil.sendJsonMessage(response,jsonData);
		}

     return null;
	}

	@GetMapping("/token")
      public JsonData getSubmitToken(){
		Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
		String token = CommonUtil.getStringNumRandom(32);
		String submitToken = CommonUtil.MD5(String.format(RedisKey.SUBMIT_ORDER_KEY,accountNo,token));
	    redisTemplate.opsForValue().set(submitToken,String.valueOf(Thread.currentThread().getId()),30, TimeUnit.MINUTES);
		return JsonData.buildSuccess(submitToken);
 }

}

